<?php

namespace backend\modules\rbac\models;

use Yii;
use common\models\rbac\Menu;

/**
 * Description of UpdateIcon
 *
 * @author emhome
 */
class MenuForm extends \common\base\FormModel {

    public $state;
    public $text;
    public $icon;

    /**
     * @var string 数据原型模型
     */
    protected $modelClass = Menu::class;

    public function rules() {
        return [
            [['text', 'icon'], 'string'],
            [['text'], 'required', 'on' => 'text'],
            [['icon'], 'required', 'on' => 'icon'],
        ];
    }

    public function scenarios() {
        $scenarios = parent::scenarios();
        $scenarios['text'] = ['text'];
        $scenarios['icon'] = ['icon'];
        return $scenarios;
    }

    public function setExtraAttributes($model) {
        $this->attributes = $model->toArray();
    }

    public function save() {
        if (!$this->validate()) {
            return $this->dumpError();
        }
        $model = $this->getModel();
        $model->setAttributes([
            'name' => $this->text,
            'icon' => $this->icon,
        ]);
        if ($model->save()) {
            return $this->attributes;
        }
        return false;
    }

}
